<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {margin: 0;padding: 0;}
    div {
      width: 100px;
      height: 100px;
      text-align: center;
      line-height: 100px;
      margin: 20px;
      cursor: pointer;
      background-color: #f40;
    }
  </style>
</head>
<body>
  <div>1</div>
  <div>2</div>
  <div>3</div>
  <script>
    let divs = document.querySelectorAll('div');
    [...divs].forEach((item,index) => {
      item.addEventListener('click',function() {
        console.log(index)
      })
    })
  </script>
</body>
</html>
